import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Dialog Demo',
      home: Scaffold(
        appBar: AppBar(title: const Text('对话框示例')),
        body: const DialogExample(),
      ),
    );
  }
}

class DialogExample extends StatefulWidget {
  const DialogExample({super.key});

  @override
  State<DialogExample> createState() => _DialogExampleState();
}

class _DialogExampleState extends State<DialogExample> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
        child: const Text("显示删除确认对话框"),
        onPressed: () async {
          // 弹出对话框并等待其关闭
          bool? delete = await showDeleteConfirmDialog();
          if (delete == null) {
            print("取消删除");
            ScaffoldMessenger.of(context).showSnackBar(
              const SnackBar(content: Text("您已取消删除")),
            );
          } else if (delete) {
            print("已确认删除");
            ScaffoldMessenger.of(context).showSnackBar(
              const SnackBar(content: Text("文件已删除")),
            );
            // 这里可以添加实际删除逻辑
          }
        },
      ),
    );
  }

  // 删除确认对话框
  Future<bool?> showDeleteConfirmDialog() {
    return showDialog<bool>(
      context: context,
      barrierDismissible: false, // 点击对话框外部不关闭
      builder: (context) {
        return AlertDialog(
          title: const Text("提示"),
          content: const Text("您确定要删除当前文件吗?此操作不可撤销！"),
          actions: <Widget>[
            TextButton(
              child: const Text("取消"),
              onPressed: () => Navigator.of(context).pop(), // 关闭对话框
            ),
            TextButton(
              child: const Text("删除", style: TextStyle(color: Colors.red)),
              onPressed: () {
                Navigator.of(context).pop(true); // 关闭对话框并返回true
              },
            ),
          ],
        );
      },
    );
  }
}